package com.hll.iot.listener;

import cn.hutool.core.lang.Dict;
import com.aos.iot.protocol.hll.client.HllClientBox;
import com.aos.iot.socket.common.annotation.OnCmdEvent;
import com.aos.iot.socket.common.annotation.OnDisconnect;
import com.aos.iot.protocol.hll.constant.HllCmd;
import com.aos.iot.protocol.hll.client.HllClient;
import com.aos.iot.protocol.hll.message.HllMessage;
import com.hll.iot.HllMessageSendHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;



@Slf4j
@Component
@RequiredArgsConstructor
public class HllMessageListener {

    private final HllClientBox hllClientBox;

    private final HllMessageSendHelper hllMessageSendHelper;

    @OnCmdEvent(protocol = "HLL", cmd = {HllCmd.LOGIN, HllCmd.HEARTBEAT})
    public void onHeartbeat(HllClient client, HllMessage message) {
        client.send(message);
        HllMessage c = new HllMessage();
//        c.setProtocol("HLL");
        c.setHead("55aa");
        c.setVersion("00");
        c.setEncrypt("00");
        c.setCmd("06");
        c.setSn("07");
        c.setData(Dict.create()
                .set("PILE_NO", "0100000022")
                .set("PIPE_NO", "02")
                .set("PIPE_INPUT_COIL_NO", "0300000163")
                .set("PIPE_BATTERY_NO", "0622")
                .set("PIPE_CHARGE_TIME", "0003")
                .set("PIPE_CHARGE_ORDER_NO", "15f38ddf83001003"));
        client.send(c);
    }


    @OnDisconnect(protocol = "HLL")
    public void onDisconnect(HllClient client) {
        hllClientBox.remove(client.getClientId());
        hllClientBox.remove(client.getChannel());
    }

}
